import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

public class Singleton {
    private String testField;
    private String testField2;

    private Singleton() {
    }

    private static Singleton instance = null;

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    // 1
                    Singleton newInstance = new Singleton();
                    // 2
                    newInstance.testField = "xxx";
                    // 3
                    Singleton.instance = newInstance;
                    // 4
                    newInstance.testField2 = "yyy";

                }
            }
        }
        return instance;
    }



    public static void main(String[] args) {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Singleton instance = Singleton.getInstance();
                System.out.println(Thread.currentThread().getName() + ":" + instance.testField2.toString());
            }
        });
        thread.setName("test thread 1");
        thread.start();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                Singleton instance = Singleton.getInstance();
                System.out.println(Thread.currentThread().getName() + ":" + instance.testField2.toString());
            }

        });
        thread1.setName("test thread 2");
        thread1.start();

        LockSupport.park();


    }
}